# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_bootstrap=0

_realname=cmake
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.25.2
pkgrel=1
pkgdesc="A cross-platform open-source make system (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.cmake.org/"
license=("spdx:MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             $( (( _bootstrap )) || echo "${MINGW_PACKAGE_PREFIX}-cmake")
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-bzip2"
             "${MINGW_PACKAGE_PREFIX}-ncurses"
             "${MINGW_PACKAGE_PREFIX}-xz"
             "${MINGW_PACKAGE_PREFIX}-zstd"
             $([[ ${CARCH} != i686 ]] && echo \
               "${MINGW_PACKAGE_PREFIX}-qt6-base" || echo \
               "${MINGW_PACKAGE_PREFIX}-qt5-base" \
               "${MINGW_PACKAGE_PREFIX}-qt5-winextras")
             $([[ ${MSYSTEM} == CLANGARM64 ]] || echo "${MINGW_PACKAGE_PREFIX}-emacs")
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* && ${MSYSTEM} != CLANG64 ]] || echo \
               "${MINGW_PACKAGE_PREFIX}-fc"))
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-pkgconf"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-libarchive"
         "${MINGW_PACKAGE_PREFIX}-libuv"
         "${MINGW_PACKAGE_PREFIX}-ninja"
         "${MINGW_PACKAGE_PREFIX}-rhash"
         "${MINGW_PACKAGE_PREFIX}-zlib")
optdepends=("${MINGW_PACKAGE_PREFIX}-emacs: for cmake emacs mode")
if [[ ${CARCH} != i686 ]]; then
  optdepends+=("${MINGW_PACKAGE_PREFIX}-qt6-base: CMake Qt GUI")
else
  optdepends+=("${MINGW_PACKAGE_PREFIX}-qt5-winextras: CMake Qt GUI")
fi
source=("https://github.com/Kitware/CMake/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-Disable-response-files-for-MSYS-Generator.patch"
        "0002-Do-not-install-Qt-bundle-in-cmake-gui.patch"
        "0003-Fix-Python-SOABI.patch"
        "0004-Output-line-numbers-in-callstacks.patch"
        "0005-Default-to-ninja-generator.patch")
sha256sums=('c026f22cb931dd532f648f087d587f07a1843c6e66a3dfca4fb0ea21944ed33c'
            '25793edcbac05bb6d17fa9947b52ace4a6b5ccccf7758e22ae9ae022ed089061'
            'f6cf6a6f2729db2b9427679acd09520af2cd79fc26900b19a49cead05a55cd1a'
            '58b70a3c9108bb3a8fffd99e397262c9e953a53fb7a6fd360a8874908db438d3'
            '15fdf3fb1a0f1c153c8cfbdd2b5c64035b7a04de618bfe61d7d74ced0d6a5c4b'
            '426818278090704d2a12f62ef3dfd94c47b11fa2784bb842989b7f6a09ee7aa2')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #


prepare() {
  cd ${_realname}-${pkgver}
  apply_patch_with_msg \
    0001-Disable-response-files-for-MSYS-Generator.patch \
    0002-Do-not-install-Qt-bundle-in-cmake-gui.patch \
    0003-Fix-Python-SOABI.patch \
    0004-Output-line-numbers-in-callstacks.patch

  # We want cmake to default to something useful and not MSVC
  apply_patch_with_msg \
    0005-Default-to-ninja-generator.patch

  find . -name "*.orig" -exec rm -f {} \;
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  if (( _bootstrap )); then
    if [[ ${CARCH} != i686 ]]; then
      local -a _qtconfig=("--qt-gui" "--qt-qmake=${MINGW_PREFIX}/bin/qmake6.exe")
    else
      local -a _qtconfig=("--qt-gui" "--qt-qmake=${MINGW_PREFIX}/bin/qmake.exe")
    fi

    MSYSTEM=MINGW FC=${MINGW_PREFIX}/bin/gfortran.exe \
      "${srcdir}"/${_realname}-${pkgver}/configure  \
      --prefix=${MINGW_PREFIX}                      \
      --datadir=share/cmake                         \
      --docdir=share/doc/cmake                      \
      --mandir=share/man                            \
      --system-libs                                 \
      --parallel=${NUMBER_OF_PROCESSORS}            \
      --sphinx-man --sphinx-html                    \
      --bootstrap-system-jsoncpp                    \
      --bootstrap-system-libuv                      \
      --bootstrap-system-librhash                   \
      ${_qtconfig[@]}

    make
  else
    if check_option "debug" "y"; then
      _build_type="Debug"
    else
      _build_type="Release"
    fi
    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
      -GNinja \
      -DCMAKE_BUILD_TYPE=${_build_type} \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_DATA_DIR=share/cmake \
      -DCMAKE_DOC_DIR=share/doc/cmake \
      -DCMAKE_MAN_DIR=share/man \
      -DCMAKE_INFO_DIR=share/info \
      -DCMAKE_USE_SYSTEM_LIBRARIES=ON \
      -DSPHINX_MAN=ON \
      -DSPHINX_HTML=ON \
      -DBUILD_QtDialog=ON \
      -DBUILD_CursesDialog=ON \
      -DBUILD_TESTING=OFF \
      ../${_realname}-${pkgver}
    ${MINGW_PREFIX}/bin/cmake.exe --build .
  fi
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  if (( _bootstrap )); then
    ./bin/ctest.exe -j$(($(nproc)+1))
  else
    ${MINGW_PREFIX}/bin/cmake -DBUILD_TESTING=ON ../${_realname}-${pkgver}
    ${MINGW_PREFIX}/bin/cmake --build .
    ${MINGW_PREFIX}/bin/ctest.exe -j$(($(nproc)+1)) || msg2 "Tests failed"
  fi
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  if (( _bootstrap )); then
    ./bin/cmake.exe \
      -DCMAKE_INSTALL_PREFIX:PATH=${pkgdir}${MINGW_PREFIX} \
      -DCMAKE_INSTALL_LOCAL_ONLY:BOOL=OFF -P cmake_install.cmake
  else
    DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .
  fi

  if [[ ${MSYSTEM} != CLANGARM64 ]]; then
    install -d "${pkgdir}${MINGW_PREFIX}"/share/emacs/site-lisp/
    ${MINGW_PREFIX}/bin/emacs -batch -f batch-byte-compile \
      "${pkgdir}${MINGW_PREFIX}"/share/emacs/site-lisp/cmake-mode.el
  fi
}
